package paytreasure.util;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.apache.commons.beanutils.Converter;


public class DateTimeConverter implements Converter{
	 private static final String DATE      = "yyyy年MM月";//"yyyy-MM-dd";  
     private static final String DATETIME  = "yyyy年MM月dd日";//"yyyy-MM-dd HH:mm:ss";  
     private static final String TIMESTAMP = "yyyy年MM月dd日HH时mm分ss秒";//"yyyy-MM-dd HH:mm:ss.SSS";
     private static final String YEARTAMP = "yyyy年MM月dd日HH时mm分";
     private static final String MMSTAMP = "yyyy-MM-dd HH:mm:ss";
     
     
     public Object convert(Class type, Object value) {
 		// TODO Auto-generated method stub
 		return toDate(type,value);
	}
     
    public static Object toDate(Class type, Object value) {  
        if (value == null || "".equals(value))  
            return null;  
        if (value instanceof String) {  
            String dateValue = value.toString().trim();  
            int length = dateValue.length();  
            if (type.equals(java.util.Date.class)) {  
                try {  
                    DateFormat formatter = null;  
                    if (length == 8) {  
                        formatter = new SimpleDateFormat(DATE, new DateFormatSymbols(Locale.CHINA));  
                        return formatter.parse(dateValue);  
                    } 
                    if (length == 11){
                    	formatter = new SimpleDateFormat(DATETIME, new DateFormatSymbols(Locale.CHINA));  
                        return formatter.parse(dateValue); 
                    }
                    if (length == 19){
                    	formatter = new SimpleDateFormat(MMSTAMP, new DateFormatSymbols(Locale.CHINA));  
                        return formatter.parse(dateValue); 
                    }
                    if (length == 20) {  
                        formatter = new SimpleDateFormat(TIMESTAMP, new DateFormatSymbols(Locale.CHINA));  
                        return formatter.parse(dateValue);  
                    }  
                    if (length <= 17) {  
                        formatter = new SimpleDateFormat(YEARTAMP, new DateFormatSymbols(Locale.CHINA));  
                        return formatter.parse(dateValue);  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return value;  
    }


}
